import com.sun.xml.internal.ws.api.pipe.NextAction;

import java.util.ArrayList;
import java.util.List;

/***
 * 基于链表实现的映射
 */
public class LinkedListMap<K, V> implements Map<K, V> {

    private class Node {
        private K key;
        private V value;
        private Node next;

        public Node(K key, V value, Node next) {
            this.key = key;
            this.value = value;
            this.next = next;
        }

        public Node(K key) {
            this(key, null, null);
        }

        public Node() {
            this(null, null, null);
        }

        @Override
        public String toString() {

            return key.toString() + ":" + value.toString();
        }
    }

    private Node dummyHead;
    private int size;

    public LinkedListMap() {
        this.dummyHead = new Node();
        size = 0;
    }


    //提供根据key值获取对应的node
    private Node getNode(K key) {
        Node node = dummyHead.next;
        while (node != null) {
            if (node.key.equals(key)) {
                return node;
            }
            node = node.next;
        }
        return null;
    }

    @Override
    public void add(K key, V value) {
        Node node = getNode(key);
        if (null == node) {
            dummyHead.next = new Node(key, value, dummyHead.next);
            size++;
        } else {
            node.value = value;//已经存在,则替换value
        }
    }

    @Override
    public V remove(K key) {
        Node preNode = dummyHead;
        while (preNode.next != null) {
            if (preNode.next.key.equals(key)) {
                break;
            }
            preNode = preNode.next;
        }
        //移除元素
        if (preNode != null) {
            Node delNode = preNode.next;
            preNode.next = delNode.next;
            delNode.next = null;
            return delNode.value;
        }
        return null;
    }

    @Override
    public boolean contains(K key) {
        return getNode(key) != null;
    }

    @Override
    public V get(K key) {
        Node node = getNode(key);
        return node == null ? null : node.value;
    }

    @Override
    public void set(K key, V value) {
        Node node = getNode(key);
        if (null == node) {
            throw new IllegalArgumentException(key + " doesn`t exsits");
        }
        node.value = value;
    }

    @Override
    public int getSize() {
        return size;
    }

    @Override
    public boolean isEmpty() {
        return size == 0;
    }

    /**
     * 测试词频
     * @param args
     */
    public static void main(String[] args) {
        List<String> words = new ArrayList<String>();
        if(FileOperation.readFile("art1.txt",words)){
            LinkedListMap<String,Integer> map = new LinkedListMap<>();
            for(String word : words){
                if(map.contains(word)){
                    map.set(word,map.get(word) + 1);
                }else{
                    map.add(word,1);
                }
            }
            System.out.println("total number : " + words.size());
            System.out.println("Word You of Number : " + map.get("You"));
            System.out.println("Word Long of Number : " + map.get("Long"));
        }
    }

}
